﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using AresCustomYacht.Code;

namespace AresCustomYacht
{
    public partial class P_ActualLabor : PageManager
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            InitializePageManager();
            if (Page.IsPostBack) return; //fire events
            Utility.ApplyMode((PageMode) _mode, new List<WebControl> {btnAdd, btnAddNShowReport},
                              new List<WebControl> {btnUpdate}, new List<WebControl> {btnDelete});
            Utility.DisplayPageMessage(this, lblMessage);

            //Get boatId anyway
            Code.Boat boat = _bl.GetBoat(BoatId);

            dfnYachtId.Text = BoatIdString;
            lblBoatName.Text = boat.BoatName;

            string taskIdString = Utility.GetQueryStringItem(this, "ParentId");

            if (_mode == 0 && !string.IsNullOrEmpty(taskIdString))
            {
                Task task = _bl.GetTask(int.Parse(taskIdString));
                dfnTaskId.Text = taskIdString;
                lblTaskName.Text = task.Code + "  " + task.Name;
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Insert();

            string linkAddress = "Mode=0&BoatId=" + BoatIdString;
            //go to Trade instead. in this case you need to keep note of the selected Division
            if (dffBookmark.Checked && !string.IsNullOrEmpty(Utility.GetQueryStringItem(this, "Bookmark")))
            {
                if (dffBookmark.Checked && !String.IsNullOrEmpty(Utility.GetQueryStringItem(this, "Bookmark")))
                    //user wants to go to book mark and there is a bookmark
                    linkAddress += "&ParentId=" +
                                   int.Parse(Utility.GetQueryStringItem(this, "BookmarkId"));
                Response.Redirect(Utility.GetQueryStringItem(this, "Bookmark") + "?k=" + Utility.Encrypt(linkAddress));
            }
            else
            {
                Response.Redirect("P-Division.aspx?k=" + Utility.Encrypt(linkAddress));
            }
        }

        private void Insert()
        {
            var actualLabor = new Tracking();
            ReflectionCopier<Tracking>.CopyControlsToObject(actualLabor, dfnTaskId.Parent);
            actualLabor.TaskId = int.Parse(dfnTaskId.Text);
            actualLabor.BoatId = BoatId;
            actualLabor.ActualHrs = dfmActualLaborHrs.ValueDecimal;
            actualLabor.ActualRate = dfmRate.ValueDecimal;
            actualLabor.InvoiceRate = dfmInvoiceRate.ValueDecimal;

            _dal.AddTracking(actualLabor);
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("Main-Reports.aspx?k=" +
                              Utility.Encrypt("Mode=0&BoatId=" + BoatIdString + "&Message=Cancelled"));
        }

        protected void btnAddNew_Click(object sender, EventArgs e)
        {
            int boatId = int.Parse(Utility.GetQueryStringItem(this, "BoatId"));
            string linkAddress = Utility.Encrypt("Mode=0&BoatId=" + boatId);
            Response.Redirect("P-Division.aspx?k=" + linkAddress);
        }

        protected void btnAddNShowReport_Click(object sender, EventArgs e)
        {
            Insert();

            int boatId = int.Parse(Utility.GetQueryStringItem(this, "BoatId"));
            string linkAddress =
                Utility.Encrypt("Mode=0&BoatId=" + boatId + "&Message=Cost added for " + lblTaskName.Text);
            Response.Redirect("Main-Reports.aspx?k=" + linkAddress);
        }

        protected void dfsTaskDescription0_TextChanged(object sender, EventArgs e)
        {
        }
    }
}